package cn.patpang.core.tool;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Title: DateTool.java
 * @Author Patrick Pang
 * @Date 2014-9-23
 * @Description 日期时间工具类
 * 
 */
public class DateTool {

	public static Date to24Hour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime();
	}

	// yyyy-MM-ddTHH:mm:ss.SSS+Z:00
	public static String parseTimeToPNet(Date dateTime) {
		return DateTool.toDate(dateTime).replace(" ", "T") + "+08:00";
	}

	public static String toDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	public static String toDate(Date datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return null == datetime ? "" : sdf.format(datetime);
	}

	public static String toDate(Date datetime, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return null == datetime ? "" : sdf.format(datetime);
	}

	public static String toDate(long datetime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(datetime);
	}

	public static String toDate(long datetime, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(datetime);
	}

	public static String toDate(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static String toDate(String format, Locale locale) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
		return sdf.format(new Date());
	}

	public static String toDate(Date datetime, String format, Locale locale) {
		SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
		return null == datetime ? "" : sdf.format(datetime);
	}

	public static String getEihiDate() {
		int mm = Integer.valueOf(toDate("MM"));
		if (mm < 5) {
			return toDate("yyyy");
		} else {
			return String.valueOf(Integer.valueOf(toDate("yyyy")) + 1);
		}
	}

	public static boolean isValidDate(String str, String formatStr) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		try {
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) {
			convertSuccess = false;
		}
		return convertSuccess;
	}

	public static Date parseDate(String str, String formatStr) {
		Date convertSuccess;
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		try {
			format.setLenient(false);
			convertSuccess = format.parse(str);
		} catch (ParseException e) {
			convertSuccess = null;
		}
		return convertSuccess;
	}

	/**
	 * 获取截至今天0.0.0
	 * 
	 * @return
	 */
	public static Date getCurrDateStartTime() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		return ca.getTime();
	}

	/**
	 * 获取截至某天的0.0.0
	 * @param date
	 * @return
	 */
	public static Date getDateStartTime(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		return ca.getTime();
	}

	/**
	 * 获取截至今天23.59.59
	 * 
	 * @return
	 */
	public static Date getCurrDateEndTime() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		return ca.getTime();
	}

	/**
	 * 获取截至某天的23.59.59
	 * @param date
	 * @return
	 */
	public static Date getDateEndTime(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		return ca.getTime();
	}

	/**
	 * 获取本周第一天（周一）
	 * 
	 * @return
	 */
	public static Date getCurrWeekFristDay() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		return ca.getTime();
	}

	/**
	 * 获取本周第一天（周日）
	 * 
	 * @return
	 */
	public static Date getCurrWeeklastDay() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.HOUR_OF_DAY, 59);
		ca.set(Calendar.HOUR_OF_DAY, 59);
		return ca.getTime();
	}

	/**
	 * 字符串转换成时间
	 * 
	 * @param stringDate
	 * @return
	 */
	public static Date string2Date(String stringDate, String pas) {
		Date reDate;

		try {
			reDate = new SimpleDateFormat(pas).parse(stringDate);
		} catch (ParseException e) {
			e.printStackTrace();
			reDate = null;
		}

		return reDate;
	}

	/**
	 * 字符串转换成时间
	 * 
	 * @param stringDate
	 * @return
	 */
	public static Date string2Date(String stringDate) {
		return string2Date(stringDate, "yyyyMMdd");
	}

	/**
	 * 日期添加
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date, int days) {
		if (null == date) {
			date = new Date();
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(Calendar.DATE, days); // minus number would decrement the days
		return cal.getTime();
	}

	/**
	 * 时间添加
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addHour(Date date, int Hour) {

		if (null == date) {
			date = new Date();
		}

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(Calendar.HOUR_OF_DAY, Hour);
		return cal.getTime();
	}

	/**
	 * Date类型转换成Calendar类型
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Calendar toCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * 获取一个月内的周末
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List getWeekendOfMonth(Date date, String fomat) {
		if (date == null) {
			return null;
		}
		List weekendList = new ArrayList();
		for (int i = 1; i <= 30; i++) {
			Calendar calendar = DateTool.toCalendar(DateTool.addDays(date, i));
			int week = calendar.get(Calendar.DAY_OF_WEEK);
			if (week == 1 || week == 7) {
				weekendList.add(DateTool.toDate(calendar.getTime(), fomat));
			}
		}
		return weekendList;
	}

	/**
	 * 获取一个月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date getMonthLastDay(Integer year, Integer month) {

		if (0 == month) {
			Calendar cal = Calendar.getInstance();
			// 设置年份
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 1);
			return cal.getTime();
		} else {
			Calendar cal = Calendar.getInstance();
			// 设置年份
			cal.set(Calendar.YEAR, year);
			// 设置月份
			cal.set(Calendar.MONTH, month - 1);
			// 获取某月最大天数
			int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			// 设置日历中月份的最大天数
			cal.set(Calendar.DAY_OF_MONTH, lastDay);
			return cal.getTime();
		}

	}

	/**
	 * 获取一个月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getMonthLastDayStr(Integer year, Integer month) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(getMonthLastDay(year, month));
	}

	/**
	 * 获取当月第一天
	 * 
	 * @return
	 */
	public static Date getMonthFristDay(int month) {
		Calendar ca = Calendar.getInstance();
		ca.set(ca.get(Calendar.YEAR), (month - 1), 1, 0, 0, 0);
		return ca.getTime();
	}

	/**
	 * 获取当月最后一天
	 * 
	 * @return
	 */
	public static Date getMonthLastDay(int month) {
		Calendar ca = Calendar.getInstance();
		// ca.set(ca.get(Calendar.YEAR), (month - 1),
		// ca.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
		ca.set(Calendar.MONTH, month - 1);
		int lastDay = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
		ca.set(Calendar.DAY_OF_MONTH, lastDay);
		return ca.getTime();
	}

	/**
	 * 获取当月第一天
	 * 
	 * @return
	 */
	public static Date getCurrMonthFristDay() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.MONTH, 0);
		ca.set(Calendar.DAY_OF_MONTH, 1);
		return ca.getTime();
	}

	/**
	 * 获取当月最后一天
	 * 
	 * @return
	 */
	public static Date getCurrMonthLastDay() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		return ca.getTime();
	}

	/**
	 * 获取当年
	 * 
	 * @return
	 */
	public static int getCurrYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	/**
	 * 获取当月
	 * 
	 * @return
	 */
	public static int getCurrMonth() {
		return Calendar.getInstance().get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取当天
	 * 
	 * @return
	 */
	public static int getCurrDay() {
		return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取当前小时
	 * 
	 * @return
	 */
	public static int getCurrHour() {
		return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
	}

	public static void main(String[] args) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(format.format(getCurrDateStartTime()));
		System.out.println(format.format(getCurrDateEndTime()));
		System.out.println(getCurrYear());
		System.out.println(getCurrMonth());
		System.out.println(getCurrDay());
	}

	/**
	 * @param date1 时间1
	 * @param date2 时间2
	 * @return
	 */
	public static int compareDate(String date1, String date2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 传入一个时间 比较这个时间在当前时间的前后
	 * @param dt1
	 * @return
     */
	public static int compareDate(Date dt1) {

		try {
			Date dt2 = new Date();
			if (dt1.getTime() > dt2.getTime()) {
				return 1;  //后
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1; //前
			} else {
				return 0; //相等
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 比较两个提起相差天数
	 * @param smdate
	 * @param bdate
     * @return
     */
	public static int daysBetween(String smdate, String bdate, String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		long time1 = 0;
		long time2 = 0;

		try{
			cal.setTime(sdf.parse(smdate));
			time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			time2 = cal.getTimeInMillis();
			}catch(Exception e){
				e.printStackTrace();
			}
		long between_days=(time2-time1)/(1000*3600*24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static Date dateToDate(Date date, String format){
		DateFormat df = new SimpleDateFormat(format);
		Date newDate = new Date();

		try {
			newDate = df.parse(df.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return newDate;
	}
}